require(['init'], function () {
      if(!localStorage.lang){
        localStorage.setItem('lang','zh');
      }
      if(localStorage.lang && localStorage.lang=='zh'){
        $('body').append('<script src="codebase/locale/locale_cn.js"  type="text/javascript" charset="utf-8"></script>');
        $('.flow-zh').show();
      }
     else {
        $('body').append('<script src="codebase/locale/locale_en.js"  type="text/javascript" charset="utf-8"></script>');
        $('.flow-en').show();
      }
  $('#flow-map area').on('click', function () {
    var activeLink = $(this).attr('href')
    parent.window.$('#js-leftcontent').find('[data-index]').each(function () {
      var $this = $(this)
      if($this.attr('data-index') == activeLink) {
        $this.addClass('active').siblings().removeClass('active')
      }
    })
  })

  function getCourseType() {
    ajaxMethod({
      url: '/api/coursetypes',
      success: function (data) {
        loadSchoolLessonTime(data)
      }
    })
  }

  function loadSchoolLessonTime(data) {
    var courseList = [
      {
        key: 0,
        label:i18next.t('table.pleasecourse')//请选择课程类型
      }
    ];

    $.each(data, function (i, v) {
      courseList.push({
        key: v.coursetypeuid,
        label: v.coursetypename,
        color: v.colorvalue
      })
    });

    var lightbox = [
      {
        name: i18next.t('table.totalquota'),//总名额
        type: "template",
        map_to: "quotas",
        height: 30
      },
      {
        name: i18next.t('table.quotaused'),//已用名额
        type: "template",
        map_to: "usequotas",
        height: 30
      },
      {
        name: i18next.t('course.coursetype'),//课程类型
        type: "template",
        map_to: "coursetype",
        // options: courseList,
        default_value: '0',
        height: 30
      },
      {
        name: i18next.t('table.lessonsname'),//课节名称
        type: "template",
        map_to: "lessonname",
        height: 30,
        default_value: ''
      },
      {
        name: i18next.t('table.classresources'),//课节资源
        type: "template",
        map_to: "foldername"
      },
      {
        name: i18next.t('info.studentlist'),//学生列表
        type: "template",
        map_to: "students",
        height: 30
      },
      {
        name: i18next.t('table.auditor'),//旁听生
        type: "template",
        map_to: "auditors"
      },
      {
        name: "time",
        type: "time",
        map_to: "auto",
        height: 30
      }
    ]

    schedulerInit({
      url: '/api/schedule',
      schedulerType: 3,
      courseList: courseList,
      create_lightbox: lightbox,
      drag_move: false,
      buttons_left: [],
      buttons_right: ['dhx_cancel_btn'],
      icons_select: ['icon_details'],
      processData: function (data, courseList) {
        var dataArr = [];
        $.each(data, function (i, v) {
          var studentArr = v.students || [];
          var studentNameArr = [];
          $.each(studentArr, function () {
            studentNameArr.push(this.name);
          });

          //旁听
          var auditArr = v.auditors || [];
          var auditNameArr = [];
          $.each(auditArr, function () {
            auditNameArr.push(this.name);
          });

          var cur = {
            start_date: new Date(formatTime(v.begintime)),
            end_date: new Date(formatTime(v.endtime)),
            text: v.teacherName,
            quotas: v.quotas || '0',
            usequotas: v.usequotas || '0',
            students: studentNameArr.join(', ') || i18next.t('table.empty'),
            teachermobile: v.teachermobile,
            teachername: v.teachername,
            color: getCourseColorById(courseList, v.coursetypeid) || '#96d6e3',
            lessonname: v.name,
            coursetype: v.coursetypename,
            foldername: v.foldername,
            auditors: auditNameArr.join(', ') ||i18next.t('table.empty')
          }


          if (cur.quotas != '0' && cur.quotas === cur.usequotas) {
            cur.color = 'red';
          }

          dataArr.push(cur);
        })
        return dataArr;
      }
    })
  }
  function getGuestList() {
    ajaxMethod({
      url: '/api/marketing/leads',
      data: {
        page: 0,
        perPage: 10
      },
      success: function (data, status) {
        ajaxMethod({
          url: "/api/orders",
          type: "get",
          success:function(json){
            if(status.code == 0) {
              var $tableList = $('#js-guestlist').html('')
              var $thead = $('<thead>\
                <tr>\
                 <th>'+i18next.t('table.no')+'</th>\
                 <th>'+i18next.t('table.registration')+'</th>\
                 <th>'+i18next.t('table.markettile')+'</th>\
                 <th>'+i18next.t('table.name')+'</th>\
                 <th>'+i18next.t('table.phone')+'</th>\
                 <th>'+i18next.t('table.service')+'</th>\
                 <th>'+i18next.t('info.classinaccountnumber')+'</th>\
                 <th>'+i18next.t('table.purchaserecord')+'</th>\
                 <th>'+i18next.t('table.coursearrangement')+'</th>\
                </tr>\
              </thead>').appendTo($tableList)
              if(!data.list.length) {
                $tableList.parent().append('<div style="margin-top:50px;text-align:center">'+i18next.t('input.nullresults')+'</div>')
                return
              }
              var $tbody = $('<tbody></tbody>').appendTo($tableList)
              $.each(data.list, function (i, item) {
                var $tr = $('<tr></tr>')
                $tr.append('<td>'+(i+1)+'</td>')
                $tr.append('<td>'+ (item.created_at||'') +'</td>')
                $tr.append('<td>'+ (item.posttitle||'') +'</td>')
                $tr.append('<td>'+ (item.name||'') +'</td>')
                $tr.append('<td>'+ item.number +'</td>')
                $tr.append('<td>'+ (item.staff==null?"--":item.staff) +'</td>')
                $tr.append('<td>'+ (item.ClassInNo==null?"--":item.ClassInNo) +'</td>')
                var $purchase=$('<td></td>').appendTo($tr);
                $tr.append('<td>'+ (item.schedule==0?i18next.t('table.notimetable'):i18next.t('table.scheduledcourses')) +'</td>');
                var flag = false;
                $.each(json,function(int,js){
                  if(item.number==js.mobile){
                      var $hasy = $('<a style="color:#398dee">'+i18next.t('table.haverecords')+'</a>').appendTo($purchase);
                      flag = true;
                      return false;
                  }
                })
                if (!flag) {
                    $('<a>'+i18next.t('table.norecord')+'</a>').appendTo($purchase)
                }
                $tbody.append($tr)
              })
            }
          }
        })
      }
    })
  }
  getCourseType();
  getGuestList();
})
